View Javadoc
1   package edu.jiangxin.apktoolbox.convert.color;
2   
3   import edu.jiangxin.apktoolbox.convert.color.colortable.IColorTable;
4   import edu.jiangxin.apktoolbox.convert.color.colortable.OrdinaryColorTable;
5   import edu.jiangxin.apktoolbox.convert.color.colortable.RalColorTable;
6   import edu.jiangxin.apktoolbox.convert.color.utils.ColorUtils;
7   import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
8   import edu.jiangxin.apktoolbox.utils.Constants;
9   
10  import javax.swing.*;
11  import javax.swing.table.DefaultTableCellRenderer;
12  import javax.swing.table.DefaultTableModel;
13  import java.awt.*;
14  import java.io.Serial;
15  
16  public class ColorTablePanel extends EasyPanel {
17      @Serial
18      private static final long serialVersionUID = 1L;
19  
20      private JComboBox<IColorTable> colorTableTypeComboBox;
21  
22      private JTable colorTable;
23  
24      public ColorTablePanel() throws HeadlessException {
25          super();
26      }
27  
28      @Override
29      public void initUI() {
30          BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
31          setLayout(boxLayout);
32  
33          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
34          createColorTablePanel();
35      }
36  
37      private void createColorTablePanel() {
38          JPanel colorTablePanel = new JPanel();
39          add(colorTablePanel);
40          setLayout(new BorderLayout());
41          setBorder(BorderFactory.createTitledBorder("Color Table"));
42  
43          JPanel yPanel = new JPanel();
44          add(yPanel);
45          yPanel.setLayout(new BoxLayout(yPanel, BoxLayout.Y_AXIS));
46  
47          colorTableTypeComboBox = new JComboBox<>();
48          IColorTable ordinaryColorTable = new OrdinaryColorTable();
49          IColorTable ralColorTable = new RalColorTable();
50          colorTableTypeComboBox.addItem(ordinaryColorTable);
51          colorTableTypeComboBox.addItem(ralColorTable);
52          colorTableTypeComboBox.setSelectedItem(ordinaryColorTable);
53          colorTableTypeComboBox.addItemListener(itemEvent -> {
54              ColorDefaultTableModel model = (ColorDefaultTableModel) colorTable.getModel();
55              IColorTable selectedColorTable = (IColorTable) itemEvent.getItem();
56              model.setDataVector(selectedColorTable.getTableRowData(), selectedColorTable.getColumnNames());
57              onColorTableChanged();
58          });
59  
60          IColorTable selectedColorTable = (IColorTable) colorTableTypeComboBox.getSelectedItem();
61          colorTable = new JTable(new ColorDefaultTableModel(selectedColorTable.getTableRowData(), selectedColorTable.getColumnNames()));
62          JScrollPane colorTableScrollPane = new JScrollPane(colorTable);
63          onColorTableChanged();
64  
65          yPanel.add(colorTableTypeComboBox);
66          yPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
67          yPanel.add(colorTableScrollPane);
68      }
69  
70      private void onColorTableChanged() {
71          IColorTable selectedColorTable = (IColorTable) colorTableTypeComboBox.getSelectedItem();
72          int labelIndex = selectedColorTable.getLabelIndex();
73          String labelName = colorTable.getColumnName(labelIndex);
74          colorTable.getColumn(labelName).setCellRenderer(new ColorTableCellRenderer());
75      }
76  
77      private class ColorDefaultTableModel extends DefaultTableModel {
78          @Serial
79          private static final long serialVersionUID = 1L;
80  
81          public ColorDefaultTableModel(Object[][] data, Object[] columnNames) {
82              super(data, columnNames);
83          }
84  
85          @Override
86          public boolean isCellEditable(int row, int column) {
87              return true;
88          }
89      }
90  
91      private class ColorTableCellRenderer extends DefaultTableCellRenderer {
92          @Serial
93          private static final long serialVersionUID = 1L;
94  
95          @Override
96          public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
97              Component renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
98              IColorTable selectedColorTable = (IColorTable) colorTableTypeComboBox.getSelectedItem();
99              if (selectedColorTable == null) {
100                 return renderer;
101             }
102             String hex = (String) colorTable.getValueAt(row, selectedColorTable.getHexIndex());
103             Color color = ColorUtils.hex2Color(hex);
104             renderer.setBackground(color);
105             return renderer;
106         }
107     }
108 }